package com.zzy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.system.domain.SaleOrderTracePictures;
import com.zzy.system.domain.SaleOrderTraceSpus;
import com.zzy.system.domain.SaleOrderTraceSpusBatches;
import com.zzy.system.mapper.SaleOrderTracePicturesMapper;
import com.zzy.system.mapper.SaleOrderTraceSpusMapper;
import com.zzy.system.service.ISaleOrderTracePicturesService;
import com.zzy.system.service.ISaleOrderTraceSpusBatchesService;
import com.zzy.system.service.ISaleOrderTraceSpusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaleOrderTraceSpusServiceImpl extends ServiceImpl<SaleOrderTraceSpusMapper, SaleOrderTraceSpus> implements ISaleOrderTraceSpusService {
    private static final Logger log = LoggerFactory.getLogger(SaleOrderTraceSpusServiceImpl.class);
    @Autowired
    private ISaleOrderTraceSpusBatchesService iSaleOrderTraceSpusBatchesService;
    @Override
    public void saveOrderTraceSpus(List<SaleOrderTraceSpus> saleOrderTraceSpusList) {
        if(!saleOrderTraceSpusList.isEmpty()){
            QueryWrapper<SaleOrderTraceSpus> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("report_id", saleOrderTraceSpusList.get(0).getReportId());
            baseMapper.delete(queryWrapper);
            this.saveBatch(saleOrderTraceSpusList);
            for(SaleOrderTraceSpus SaleOrderTraceSpus:saleOrderTraceSpusList){
                if(SaleOrderTraceSpus.getBatches()!=null&&!SaleOrderTraceSpus.getBatches().isEmpty()){
                    SaleOrderTraceSpus.getBatches().forEach(detail -> {
                        detail.setTraceSpusId(SaleOrderTraceSpus.getId());
                        detail.setReportId(SaleOrderTraceSpus.getReportId());
                    });
                    iSaleOrderTraceSpusBatchesService.saveOrderTraceSpusBatches( SaleOrderTraceSpus.getBatches());
                }

            }
        }
    }
}